﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using nwrole.toolbox.sync.entity;
using NHibernate;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate.Linq;
using nhEntity_web.Util;
using Telerik.Web.Mvc.UI;

namespace nhEntity_web.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        private static string connectionString = "";// "Data Source=146.66.234.53;Initial Catalog=nwrole;User Id=nwrole_user;Password=nwrole123!_";

        public void hej(HtmlHelper helper)
        {
            helper.Telerik();
        }

        public ActionResult Index()
        {

            // mål: att lyckad visa en grid med dynamiskt laddad data....
            
            // delmål 1: compilera en klass
            // delmål 2: lyckas ladda intrument dynamiskt
            // delmål 3: visa resultatet i en gid

            var sessionFactory = CreateSessionFactory();
            using (var session = sessionFactory.OpenSession())
            {
                IDynComp comp = nhFactory.LoadModel(session, 1);
                ViewBag.comp = comp;
            } 
            return View();
        }

        public ActionResult Index2()
        {

            var sessionFactory = CreateSessionFactory();

            using (var session = sessionFactory.OpenSession())
            {

                IQueryable<nhEntityView> result2 = session.Query<nhEntityView>().Where(t => t.Name != null).FetchMany(c => c.Columns);
                foreach (var r in result2)
                {
                    if (r.Columns != null)
                        foreach (var item in r.Columns)
                        {
                            System.Console.WriteLine(item.Id + " " + item.Name);
                        }

                    System.Console.WriteLine(r.Id + " " + r.Name);
                }

                IQueryable<nhEntity> result = session.Query<nhEntity>().Where(t => t.Name != null).FetchMany(c => c.Columns);
                foreach (var r in result)
                {
                    if (r.Columns != null)
                        foreach (var item in r.Columns)
                        {
                            System.Console.WriteLine(item.Id + " " + item.Name);
                        }

                    System.Console.WriteLine(r.Id + " " + r.Name);
                }

            }

            List<nhEntity> list = new List<nhEntity>();
            list.Add(new nhEntity()
            {
                Id = 1,
                Name = "hejsan1"
            });
            list.Add(new nhEntity()
            {
                Id = 1,
                Name = "hejsan2"
            });
            list.Add(new nhEntity()
            {
                Id = 1,
                Name = "hejsan3"
            });
            list.Add(new nhEntity()
            {
                Id = 1,
                Name = "hejsan4"
            });


            return View(list);
        }


        private static ISessionFactory CreateSessionFactory()
        {
            FluentConfiguration conf = Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2008.ConnectionString(connectionString).ShowSql().AdoNetBatchSize(0))
                .Mappings(m =>
                    m.FluentMappings.AddFromAssemblyOf<nhEntity>());
               // .BuildConfiguration().AddAssembly("jhjhhj");
            // conf.ExposeConfiguration(BuildSchema);
            
            return conf.BuildSessionFactory();
        }

/*        private static void BuildSchema(Configuration config)
        {
            config.EventListeners.PreUpdateEventListeners = new IPreUpdateEventListener[] { new MyInsertEventListener() };
            config.SetInterceptor(new MyInterceptor());
            NHibernate.Tool.hbm2ddl.SchemaExport schema = new NHibernate.Tool.hbm2ddl.SchemaExport(config);
            // schema.Create(false, true);
        } */

    }
}
